package com.windowes.weichang.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.windowes.weichang.common.ServerResponse;
import com.windowes.weichang.dao.RuleMapper;
import com.windowes.weichang.dao.SplashMapper;
import com.windowes.weichang.dao.WDistrictMapper;
import com.windowes.weichang.dto.RuleDto;
import com.windowes.weichang.pojo.Rule;
import com.windowes.weichang.pojo.Splash;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/***
 *
 * @author: yangqing
 * @email: qing.yang@bangdao-tech.com
 * @createTime: 2018/6/16 下午5:19
 * @description:
 *
 **/
@Service
public class RuleService {


    @Autowired
    RuleMapper ruleMapper;
    @Autowired
    SplashMapper splashMapper;

    @Autowired
    private WDistrictMapper districtMapper;

    /**
     * @return
     */
    public ServerResponse addOrUpdate(RuleDto feedback) {
        if (feedback == null) {
            return ServerResponse.createByErrorMessage("操作失败!");
        }

        if (StringUtils.isEmpty(feedback.getContent())) {
            return ServerResponse.createByErrorMessage("请提交内容信息!");
        }

        Rule rule = new Rule();
        BeanUtils.copyProperties(feedback, rule);
        List<Splash> splashs = feedback.getSplashs();
        if (feedback.getId() == null) {
            ruleMapper.insertSelective(rule);
            for (Splash splash : splashs) {
                splash.setId(null);
                splash.setRuleId(rule.getId());
                splashMapper.insertSelective(splash);
            }
        } else {
            ruleMapper.updateByPrimaryKeySelective(rule);
            //删除所有配置规则
            splashMapper.deleteByRuleId(rule.getId());
            for (Splash splash : splashs) {
                splash.setId(null);
                splash.setRuleId(rule.getId());
                splashMapper.insertSelective(splash);
            }
        }
        return ServerResponse.createBySuccessMessage("操作成功!");
    }


    public ServerResponse delete(Integer id) {
        if (id == null) {
            return ServerResponse.createBySuccessMessage("请提交id!");
        }
        ruleMapper.deleteByPrimaryKey(id);
        return ServerResponse.createBySuccessMessage("操作成功!");
    }


    /**
     * 加载规则列表
     *
     * @param type
     * @param pageNo
     * @param pageSize
     * @return
     */
    public ServerResponse list(Integer type, int pageNo, int pageSize) {
        Page<Rule> page = new Page<>(pageNo, pageSize);
        List<Rule> records = ruleMapper.selectByPage(page);
        Date current = new Date();
        for (Rule rule : records) {
            //处理生效中
            rule.setEffectStatus(belongCalendar(current, rule.getEffectTime(), rule.getAbateTime()));
            String cityIds = rule.getCityIds();
            if (!StringUtils.isBlank(cityIds)) {
                String city = districtMapper.selectByCityIds(cityIds.split(","));
                rule.setCity(city);
            }
        }
        page.setRecords(records);
        return ServerResponse.createBySuccess(page);
    }

    /**
     * 判断time是否在from，to之内
     *
     * @param time 指定日期
     * @param from 开始日期
     * @param to   结束日期
     * @return
     */
    public static int belongCalendar(Date time, Date from, Date to) {
        Calendar date = Calendar.getInstance();
        date.setTime(time);

        Calendar after = Calendar.getInstance();
        after.setTime(from);

        Calendar before = Calendar.getInstance();
        before.setTime(to);

        if (date.after(after) && date.before(before)) {
            //落在时间段内
            return 0;
        } else {
            return date.before(before) ? 1 : -1;
        }
    }

    public ServerResponse item(Integer id) {
        if (id == null) {
            return ServerResponse.createByErrorMessage("请提交id!");
        }

        Rule rule = ruleMapper.selectByPrimaryKey(id);
        if (rule == null) {
            return ServerResponse.createByErrorMessage("该规则已被删除,请核实!");
        }
        RuleDto ruleDto = new RuleDto();
        BeanUtils.copyProperties(rule, ruleDto);
        List<Splash> splashs = splashMapper.selectByRuleId(rule.getId());
        ruleDto.setSplashs(splashs);
        return ServerResponse.createBySuccess("获取成功!", ruleDto);
    }

}